﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.Diagnostics;
using System.Xml.Serialization;
using System.IO;

namespace Curling
{
    [DataContractAttribute]
    public class GameData
    {
        [DataMemberAttribute]
        public ObservableCollection<StoneReplayFrame> StonesGameFrame { get; set; }

        private string StoneFramesXml;

        private const string Header = "<?xml version=\"1.0\" encoding=\"utf-16\"?><GameData xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/Curling\">";
        private const string Footer = "</GameData>";

        public GameData()
        {
            StonesGameFrame = new ObservableCollection<StoneReplayFrame>();

            for (int i = 0; i < GameManager.StonesCount; i++)
            {
                StonesGameFrame.Add(null);
            }
        }

        public void AddStoneFrame(StoneEntity stone/*ReplayFrame StoneGameFrame*/)
        {
            //if (StoneGameFrames == null)
            //    StoneGameFrames = new ObservableCollection<StoneReplayFrame>();

            //StoneGameFrames.Add(StoneGameFrame);

            //DateTime t = DateTime.Now;

            StringBuilder stringBuilder = new StringBuilder();

            StoneReplayFrame StoneGameFrame = new StoneReplayFrame() 
            {
                X = stone.X,
                Y = stone.Y,
                Rotation = stone.Rotation,
                State = stone.State
            };

            //XmlDictionaryWriter xmlWriter = XmlDictionaryWriter.CreateDictionaryWriter(Create(stringBuilder);
            using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, new XmlWriterSettings() { Indent = true, OmitXmlDeclaration = true }))
            {
                DataContractSerializer dtaContractSerializer = new DataContractSerializer(typeof(StoneReplayFrame));
                dtaContractSerializer.WriteObject(xmlWriter, StoneGameFrame);
            }

            string xml = stringBuilder.ToString();
            //Debug.WriteLine(xml);
            StoneFramesXml += xml;

            //Debug.WriteLine(DateTime.Now.Subtract(t).TotalMilliseconds.ToString());
        }

        public string ToXml()
        {
            if (StoneFramesXml.Length > 0)
            {
                return Header + "\n<StonesGameFrame>\n" + StoneFramesXml + "\n</StonesGameFrame>\n" + Footer + "\n";
            }
            else
            {
                return "";
            }
        }

        public static GameData FromXml(string Xml)
        {
            StringReader stringReader = new StringReader(Xml);

            GameData gameData;

            using (XmlReader xmlReader = XmlReader.Create(stringReader))
            {
                DataContractSerializer dtaContractSerializer = new DataContractSerializer(typeof(GameData));
                gameData = (GameData)dtaContractSerializer.ReadObject(xmlReader);
            }

            return gameData;

            //if (gameData != null)
            //{
            //    foreach (StoneReplayFrame StoneGameFrame in gameData.StoneGameFrames)
            //    {
            //        StoneGameFrames.Add(StoneGameFrame);
            //    }
            //}
        }

        public StoneReplayFrame GetStoneFrame(int StoneID)
        {
            return StonesGameFrame[StoneID];
        }

        //public void AddFrame(StoneReplayFrame stoneFrame)
        //{
        //    StoneGameFrames.Add(stoneFrame);
        //}
    }
}